import os
import concurrent.futures
from qcloud_cos import CosConfig, CosS3Client

# 配置 COS SDK
secret_id = "AKIDYy95mGWXIo69wTs3yd5KK4auzoOjpfGc"
secret_key = "81f4FBtMbbLq7AELNDznU0LSL1SecNs9"
region = "ap-hongkong"
bucket = "hcecpod-1343049753"

config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key)
client = CosS3Client(config)



def get_existing_files(cos_folder):
    """ 获取 COS 目录下已有的所有文件 """
    existing_files = set()
    continuation_token = None  # COS 不支持 `ContinuationToken`，使用 `Marker` 替代

    while True:
        if continuation_token:
            response = client.list_objects(Bucket=bucket, Prefix=cos_folder, Marker=continuation_token)
        else:
            response = client.list_objects(Bucket=bucket, Prefix=cos_folder)

        if 'Contents' in response:
            for item in response['Contents']:
                existing_files.add(item['Key'])  # 存储文件路径

        # 判断是否还有更多数据
        if 'NextMarker' in response:
            continuation_token = response['NextMarker']
        else:
            break

    print(f"📌 已获取 COS 目录下 {len(existing_files)} 个文件")
    return existing_files


def upload_file(filename, existing_files,local_folder,cos_folder):
    """ 上传单个文件，跳过已存在的文件 """
    local_path = os.path.join(local_folder, filename)
    cos_path = cos_folder + filename  # 目标路径（包含 cos 目录前缀）

    if cos_path in existing_files:
        # print(f"⚠️ 文件已存在，跳过: {filename}")
        return

    if os.path.isfile(local_path):
        client.upload_file(Bucket=bucket, LocalFilePath=local_path, Key=cos_path)
        print(f"✅ 上传成功: {filename}")

def main():
    list_file=['draw','pattern','refer','model','pingpu','choose','multipack']
    for file_name in list_file:
        print("现在同步的文件夹是：",file_name)
        local_folder = "E:/WWW/pic/"+file_name
        cos_folder = file_name+"/"
        # 1️⃣ 获取 COS 目录下已有的文件
        existing_files = get_existing_files(cos_folder)

        # 2️⃣ 获取本地文件列表（只取未上传的文件）
        files = [f for f in os.listdir(local_folder) if os.path.isfile(os.path.join(local_folder, f))]

        # 3️⃣ 多线程并行上传
        with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:
            executor.map(lambda f: upload_file(f, existing_files,local_folder,cos_folder), files)

if __name__ == "__main__":
    main()